import os


class File:
    # 写入文本文档
    def WriteTxt(path, things):
        file = open(path, 'w', encoding='UTF-8')  # 设置文件对象
        file.write(things)  # 写入文本
        file.close()  # 关闭文本对象

    # 读取文本文档
    def ReadTxt(path):
        f = open(path, "r")  # 设置文件对象
        str = f.read()  # 获取内容
        f.close()  # 关闭文本对象
        return str  # 返回结果

    def ReadConfigFile(path):
        """
        :param path: 要读取的.config文件的目录
        :return: 格式化后的值
        """
        file = open(path, "r")
        config = {}
        while True:
            read = file.readline()
            if not read:
                break
            if read[0] == "#" or read == "\n":
                continue
            between = read.find(" ")
            config[read[0: between]] = read[between + 1: -1]
        return config

    # 获取用户主目录
    def GetHome() -> "获取用户主目录":
        return os.path.expanduser('~')

class Program:

    def RunCommandOnTerminal(command):
        File.WriteTxt("/tmp/install.sh", command)
        Program.RunTerminal("bash /tmp/install.sh")

    def RunTerminal(program):
        os.system("qterminal -e \"{}\"".format(program))

    def CheckSshService():
        if os.system("service ssh status") == 3:
            return True
        return False

    def IsSshServerOpen_X11Setting():
        config = File.ReadConfigFile("/etc/ssh/sshd_config")
        if not "X11Forwarding" in config.keys() or not config["X11Forwarding"].lower() == "yes":
            return False
        return True